﻿using System;

namespace Loki.Events
{
    public class WeakEventCallback<TListener, TArgs> : IWeakEventCallback<TArgs>
    {
        public object Listener
        {
            get
            {
                return _ListenerReference.Target;
            }
        }

        private WeakReference _ListenerReference;

        private Action<TListener, object, TArgs> _Callback;

        public WeakEventCallback(
            object P_Listener,
            Action<TListener, object, TArgs> P_Callback)
        {
            _ListenerReference = new WeakReference(P_Listener);
            _Callback = P_Callback;
        }

        public bool Invoke(object P_Sender, TArgs P_Args)
        {
            TListener L_ListenerForCallback = (TListener)_ListenerReference.Target;
            if (L_ListenerForCallback != null)
            {
                _Callback(L_ListenerForCallback, P_Sender, P_Args);
                return true;
            }

            return false;
        }
    }
}
